
#include "io430G2231.h"
#define     TXD                   BIT1                      // TXD on P1.1
#define     Bitime         104 //9600 Baud, SMCLK=1MHz (1MHz/9600)=104
 
unsigned char BitCnt;  // Bit count, used when transmitting byte
unsigned int TXByte;  // Value sent over UART when Transmit() is called
 
// Function Definitions
void Transmit(void);
void SendUART(char c); 
void SendString(char *str);
void USART_Init(void);
void delay(unsigned long value)
{
	do
        {
          value--;
        }
        while(value != 0);
}
/**************************************************************************/
void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  USART_Init();
  /* Main Application Loop */
  while(1)
  {  
      SendString("happy new week! happy new week!\r\n");
      delay(200000);
  }  
}
/**************************************************************************/
void SendString(char *str)
{
  while(*str != '\0')
  {
    SendUART(*str++);
  }
}
/**************************************************************************/
void USART_Init(void)
{
    BCSCTL1 = CALBC1_1MHZ;                    // Set range
    DCOCTL = CALDCO_1MHZ;      // SMCLK = DCO = 1MHz
 
    P1SEL |= TXD;                              //
    P1DIR |= TXD;                              //
   
    __bis_SR_register(GIE);        // interrupts enabled
}
/**************************************************************************/
void SendUART(char c)
{
    unsigned int uartUpdateTimer = 10;  // Loops until byte is sent
    while(1)
    {    
      if ((--uartUpdateTimer == 0))
      {
        TXByte = c;
        Transmit(); 
        break;
      }
    }
}
/**************************************************************************/
// Function Transmits Character from TXByte 
void Transmit()
{ 
  CCTL0 = OUT;                               // TXD Idle as Mark
  TACTL = TASSEL_2 + MC_2;               // SMCLK, continuous mode
 
  BitCnt = 0xA;                       // Load Bit counter, 8 bits + ST/SP
  CCR0 = TAR;
   
  CCR0 += Bitime;                  // Set time till first bit
  TXByte |= 0x100;                 // Add stop bit to TXByte (which is logical 1)
  TXByte = TXByte << 1;            // Add start bit (which is logical 0)
   
  CCTL0 =  CCIS0 + OUTMOD0 + CCIE; // Set signal, intial value, enable interrupts
  while ( CCTL0 & CCIE );          // Wait for TX completion
  TACTL = TASSEL_2;             // SMCLK, timer off (for power consumption)
}
/**************************************************************************/
// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
  CCR0 += Bitime;     // Add Offset to CCR0  
  if ( BitCnt == 0)     // If all bits TXed, disable interrupt
    CCTL0 &= ~ CCIE ;
  else
  {
    CCTL0 |=  OUTMOD2;                    // TX Space
    if (TXByte & 0x01)
      CCTL0 &= ~ OUTMOD2;                   // TX Mark
    TXByte = TXByte >> 1;
    BitCnt --;
  }
}

